<script setup lang="ts">
import { reactive } from 'vue';
import { contractListAPI } from '@/api/contract'
import ContractList from "@/components/list/ContractList.vue";
import { Toast } from 'vant';
    const tabs = [
        {
            type: 2,
            text: '待签约'
        },
        {
            type: 3,
            text: '履约中'
        },
        {
            type: 4,
            text: '已完成'
        },
        {
            type: 5,
            text: '已失效'
        },
    ]
    const props = defineProps({
        type: {
            type: String
        }
    })
    const state = reactive({
        type: props.type || tabs[0].type,
        contractList: [],
        loading: false,
        activeIndex: 0
    })
    if(props.type == '0') {
        tabs.unshift({
            type: 0,
            text: '全部'
        })
    }
    // tabs 切换
    const setTabList = (type) => {
        if (type == state.type) return
        state.type = type
        getContractList()
    }
    const getContractList = async() => {
        state.loading = true
        const res = await contractListAPI({
            is_contract_type: state.type,
            curr_identity: 3
        })
        if (res) {
            state.contractList = res.records
        }else{
            Toast(res.msg)
        }
        state.loading = false
        state.activeIndex++
    }
    getContractList()
</script>
<template>
    <div class="contract-tab">
        <span 
            :class="state.type == item.type ? 'active': ''"
            v-for="(item,index) in tabs" 
            :key="index"
            @click="setTabList(item.type)"
        >
            {{ item.text }}
            <i></i>
        </span>
    </div>
    <van-pull-refresh v-model="state.loading" @refresh="getContractList">     
        <van-loading v-if="state.activeIndex == 0 && state.loading">加载中...</van-loading>
        <ContractList :contractList="state.contractList" />
        <div class="wy-no-data" v-if="!state.loading && state.contractList.length==0">暂无数据</div>
    </van-pull-refresh>
   
</template>

<style scoped>
.van-pull-refresh{
    height: calc(100vh - 46px - 5rem);
    overflow: auto;
  }
  .contract-tab{
    display: flex;
  }
  .contract-tab span{
    font-size: 0.75rem;
    line-height: 0.75rem;
    font-weight: 400;
    color: #666666;
    flex: 1;
    align-items: center;
    border-bottom: 1px solid #EEEEEE;
    padding: 0.59rem 0;
    text-align: center;
    position: relative;
  }
  .contract-tab span.active{
    color: #FF9415;
  }
  .contract-tab span.active i{
    width: 1.63rem;
    height: 0.05rem;
    background: #FF9415;
    position: absolute;
    bottom: 0px;
    left: 50%;
    margin-left: -0.815rem;
  }
  .van-pull-refresh {
    height: calc(100vh - 46px - 5rem);
  }
  .van-loading {
    text-align: center;
    margin-top: 1rem;
  }
</style>